package com.bishe.psy.service.impl;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.dao.SCL90Mapper;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.SCL90;
import com.bishe.psy.service.IScl90Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-04-24
 * Time: 15:35
 */

@Slf4j
@Service
public class Scl90ServiceImpl implements IScl90Service {

    @Resource
    private SCL90Mapper scl90Mapper;

    @Override
    public void create(SCL90 scl90) {

        //1.参数校验
        if(scl90 == null || scl90.getUserId() == null || scl90.getUserId() <= 0
        || scl90.getScore() == null || scl90.getScore() <= 0 || scl90.getTotalAverage() == null || scl90.getTotalAverage() <= 0
        || scl90.getPositive_Num() == null || scl90.getPositive_Num() < 0 || scl90.getSomatization() == null || scl90.getSomatization() <= 0
        || scl90.getForced() == null || scl90.getForced() <= 0 || scl90.getRelationship() == null || scl90.getRelationship() <= 0
        || scl90.getDepression() == null || scl90.getDepression() <= 0 || scl90.getAnxiety() == null || scl90.getAnxiety() <= 0
        || scl90.getHostile() == null || scl90.getHostile() <= 0 || scl90.getTerror() == null || scl90.getTerror() <=0
        || scl90.getBigotry() == null || scl90.getBigotry() <= 0 || scl90.getMentally() == null || scl90.getMentally() <= 0
        || scl90.getOther() == null || scl90.getOther() <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //2.设置默认值
        scl90.setDeleteState((byte) 0);
        Date date = new Date();
        scl90.setCreateState(date);
        scl90.setUpdateState(date);

        //3.调用数据库，插入当前这条记录
        int row = scl90Mapper.insertSelective(scl90);

        //4.判断是否插入成功
        if(row <= 0 ) {
            log.warn("scl90测试问卷插入" + ResultCode.FAILED);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

    }

    @Override
    public SCL90 selectByIdNewResult(Long userId) {
        //1.参数校验
        if(userId == null || userId <= 0) {
            log.warn("传入id为空");
            throw new ApplicationException(AppResult.failed("传入参数为空"));
        }

        //2.根据用户id查询scl测评最新结果
        SCL90 scl90 = scl90Mapper.selectByIdNewResult(userId);

        //3.返回结果
        return scl90;
    }

    @Override
    public List<SCL90> selectByUserId(Long userId) {
        //1.参数校验
        if(userId == null || userId <= 0) {
            log.warn("传入id为空");
            throw new ApplicationException(AppResult.failed("传入参数为空"));
        }

        //2.调用数据库
        List<SCL90> scl90List = scl90Mapper.selectByUserId(userId);

        //3.返回数据
        return scl90List;

    }
}
